package finbudget.client.lists;

import java.util.ArrayList;

import com.gwtplatform.dispatch.shared.ActionException;

import finbudget.client.lists.GetListOfItems.ListType;
import finbudget.client.lists.GetListOfItems.Type;

public class ListTypeWithProperties extends ListType implements ListTypeElement {
	private static final long serialVersionUID = -8630555386174719663L;

	private ArrayList<String> properties;
	private String name;

	/** Serialization requirements */
	protected ListTypeWithProperties() {
	}

	public ListTypeWithProperties(String base, String name) {
		this.type = Type.VISIBLEITEMS;
		this.base = base;
		this.name = name;
	}

	public ArrayList<String> getProperties() {
		return properties;
	}

	public ListTypeWithProperties addProperty(String property) {
		if (property != null) {
			if (properties == null) {
				properties = new ArrayList<String>(2);
			}
			properties.add(property);
		}
		return this;
	}

	@Override
	public ListType clone() {
		ListTypeWithProperties result = new ListTypeWithProperties();
		result.base = new String(base);
		result.type = type;
		result.name = new String(name);
		if (properties != null) {
			result.properties = new ArrayList<String>(properties.size());
			for (String property : properties) {
				result.properties.add(new String(property));
			}
		}
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		boolean equals = super.equals(obj);
		if (equals) {
			equals = obj != null && obj instanceof ListTypeWithProperties;
			if (equals) {
				ListTypeWithProperties object = (ListTypeWithProperties) obj;
				if (properties == null) {
					equals = object.properties == null;
				} else {
					for (String property : properties) {
						equals = object.properties.contains(property);
						if (!equals) {
							return false;
						}
					}
					if (equals) {
						if (name == null) {
							equals = object.name == null;
						} else {
							equals = name.equals(object.name);
						}
					}
				}
			}
		}
		return equals;
	}

	@Override
	public GetListOfItemsResult getList(ListTypeVisitor visitor, GetListOfItemsAction action) throws ActionException {
		return visitor.getList(this, action);
	}

	public String getName() {
		return name;
	}

}
